//
//  GameController.swift
//  FastFinger
//
//  Created by mugua on 2018/6/26.
//  Copyright © 2018年 mugua. All rights reserved.
//

import UIKit
import WebKit

class GameController: UIViewController {
   
    @IBOutlet weak var webView: UIWebView! {
        didSet {
            webView.delegate = self
        }
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.interactivePopGestureRecognizer?.isEnabled = false
        navigationController?.setNavigationBarHidden(true, animated: animated)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        navigationController?.interactivePopGestureRecognizer?.isEnabled = true
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        navigationController?.setNavigationBarHidden(true, animated: true)
        
        landscape()
        loadWebView()
        
    }
    
    func landscape() {
        
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        appDelegate.allowRotation = true
        UIDevice.switchNewOrientation(.landscapeRight)
    }
    
    func loadWebView() {
        
        if let filepath = Bundle.main.path(forResource: "GameResource", ofType: "bundle") {
            
            let path = filepath + "/" + "index.html"
            
            let html = try? String(contentsOfFile: path, encoding: String.Encoding.utf8)
            
            webView.loadHTMLString(html!, baseURL: URL(string: path))
        }
    }
    
    @IBAction func popTap(_ sender: UIButton) {
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        appDelegate.allowRotation = false
        UIDevice.switchNewOrientation(.portrait)
        
        navigationController?.popViewController(animated: true)
    }
    
}


extension GameController: UIWebViewDelegate {
    
    func webViewDidStartLoad(_ webView: UIWebView) {
        SVProgressHUD.show()
        
    }
    
    func webViewDidFinishLoad(_ webView: UIWebView) {
        SVProgressHUD.dismiss()
    }
    
}
